package cn.samir.sudoku.adapter;

import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cn.samir.sudoku.R;
import cn.samir.sudoku.core.RankBean;
import cn.samir.sudoku.view.CircleImageView;
import cn.sharesdk.framework.utils.BitmapHelper;

public class RankListAdapter extends AdapterBase<RankBean> {
	Context context;
	public static int[] ids = new int[] { R.drawable.a3l, R.drawable.a3m,
			R.drawable.a3n, R.drawable.a3o, R.drawable.a3p, R.drawable.a3q,
			R.drawable.a3r, R.drawable.a3s, R.drawable.a3t, R.drawable.a3u };

	public RankListAdapter(Context context) {
		this.context = context;
	}
	
	
	@Override
	protected View getExView(int position, View convertView, ViewGroup parent) {
		ViewHold viewHold;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.item_rank, null);
			viewHold = new ViewHold();
			viewHold.civ = (CircleImageView) convertView
					.findViewById(R.id.rank_header);
			viewHold.tv_rank = (ImageView) convertView
					.findViewById(R.id.rank_number);
			viewHold.tv_rank2 = (ImageView) convertView
					.findViewById(R.id.rank_number2);
			viewHold.tv_nickname = (TextView) convertView
					.findViewById(R.id.tv_nickname);
			viewHold.tv_score = (TextView) convertView
					.findViewById(R.id.tv_score);
			convertView.setTag(viewHold);
		} else {
			viewHold = (ViewHold) convertView.getTag();
		}	
		
		RankBean rankBean = (RankBean) getItem(position);
		ImageLoader.getInstance().displayImage(rankBean.usericon, viewHold.civ);
		//viewHold.civ.setImageResource(R.drawable.ic_launcher);
		updateRankNumberView(viewHold, position);
		viewHold.tv_nickname.setText(rankBean.nickname);
		viewHold.tv_score.setText(rankBean.totalscore + "分");
		return convertView;
	}

	private void updateRankNumberView(ViewHold viewHold, int position) {
		if (position < 9) {
			viewHold.tv_rank.setImageResource(ids[position + 1]);// diwei
			viewHold.tv_rank2.setVisibility(View.GONE);// gaowei
		} else if (position < 98) {
			viewHold.tv_rank2.setVisibility(View.VISIBLE);
			viewHold.tv_rank.setImageResource(ids[(position + 1) / 10]);
			viewHold.tv_rank2.setImageResource(ids[(position + 1) % 10]);
		}
	}

	@Override
	protected void onReachBottom() {

	}

	class ViewHold {
		ImageView tv_rank;
		ImageView tv_rank2;
		CircleImageView civ;
		TextView tv_nickname;
		TextView tv_score;
	}

}
